package GradeManager.StudentGradeSystem.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class JwtUtils {
    @Value("${Jwt.secretKey}")
    private String secretKey;
    @Value("${Jwt.expire}")
    private Long expire;
//    创建jwt
    public String JwtBuilder(Map<String, Object> claims){
        return Jwts.builder()
                .setClaims(claims)        //载荷
                .signWith(SignatureAlgorithm.HS256, secretKey)       //设置签名算法和密钥
                .setExpiration(new Date(System.currentTimeMillis() + expire))//有效期
                .compact();
    }
//    解析jwt
    public Claims JwtParser(String Jwt){
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(Jwt)
                .getBody();
    }
}
